package edu.zut.repository;


import edu.zut.pojo.AyUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

//数据层接口继承JpaRepository，指定表对应的实体以及主键类型
//JpaSpecificationExecutor  规范执行器
public interface AyUserRepository extends JpaRepository<AyUser, Integer>, JpaSpecificationExecutor<AyUser> {


    //根据用户名称和性别查询
    List<AyUser> findByNameAndAge(String name,Integer age);

    //根据姓名模糊查询
    List<AyUser> findAyUsersByNameLike(String name);

    //查询年龄小于？的诗句
    List<AyUser> findByAgeIsLessThan(Integer age);

    //根据name查询记录数
    AyUser countByName(String name);

    //根据姓名移除数据
    void removeByName(String name);

    //@Query注解实现自定义sql   from 表对应的实体类名    JPQL  中 直接从from开始写，占位符 ?1  ?2  ?3  根据参数顺序匹配
    @Query("from AyUser where name like ?1 and gender = ?2 order by age desc")
    List<AyUser> queryByNameLike(String name,String gender);

    //nativeQuery:默认值是false，表示使用JPQL,当为true时，sql为原生sql
    @Query(value = "select * from ay_user where name like ?1 and gender = ?2 order by age desc",nativeQuery = true)
    List<AyUser> queryByNameLikeAndGender(String name,String gender);

    //将查询改为更新数据
    @Modifying
    @Query("update AyUser set name = ?1 where id = ?2")
    void updateAyUser(String name,int id);

    //将查询改为删除
    @Modifying
    @Query("delete from AyUser where name = ?1")
    void deleteByName(String name);



}
